package com.wanban.downloader.model;

import android.net.Uri;
import android.text.TextUtils;
import java.io.File;
import java.io.Serializable;
import java.util.Locale;

public class M3U8Seg implements Comparable<M3U8Seg>, Serializable {
    public static final String SEGMENT_PREFIX = "video_";
    public static final String INIT_SEGMENT_PREFIX = "init_video_";

    public String url;                         //分片url
    public String name;                        //分片名,可以自己定义
    public String parentUrl;             //分片的上级M3U8的url
    public String method;                      //加密的方式
    public String keyUri;                      //加密的url
    public String keyIV;                       //加密的IV
    public String initSegmentUri;              //MAP的url
    public String segmentByteRange;            //MAP的range

    public int index;                          //分片索引值,第一个为0
    public int sequence;                       //分片的sequence, 根据initSequence自增得到的
    public int retryCount;                     //分片的请求重试次数
    public long tsSize;                        //分片大小
    public long contentLength;                 //分片的Content-Length
    public float duration;                     //分片时长
    public boolean hasDiscontinuity;           //分片前是否有#EXT-X-DISCONTINUITY标识
    public boolean hasKey;                     //分片是否有#EXT-X-KEY
    public boolean hasInitSegment;             //分片前是否有#EXT-X-MAP

    public M3U8Seg() { }

    public void initTsAttributes(String url, float duration, int index,
                                 int sequence, boolean hasDiscontinuity) {
        this.url = url;
        name = url;
        this.duration = duration;
        this.index = index;
        this.sequence = sequence;
        this.hasDiscontinuity = hasDiscontinuity;
        tsSize = 0L;
    }

    public void setKeyConfig(String method, String keyUri, String keyIV) {
        hasKey = true;
        this.method = method;
        this.keyUri = keyUri;
        this.keyIV = keyIV;
    }


    public void setInitSegmentInfo(String initSegmentUri, String segmentByteRange) {
        hasInitSegment = true;
        this.initSegmentUri = initSegmentUri;
        this.segmentByteRange = segmentByteRange;
    }


    public String getLocalKeyUri() {
        return  "local_"+ index + ".key";
    }

    /**
     * if ts is local file, name is video_{index}.ts
     * if ts is network resource , name is starting with http or https.
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    public String getIndexName() {
        String suffixName = "";
        if (!TextUtils.isEmpty(url)) {
            Uri uri = Uri.parse(url);
            String fileName = uri.getLastPathSegment();
            if (!TextUtils.isEmpty(fileName)) {
                fileName = fileName.toLowerCase();
                suffixName = getSuffixName(fileName);
            }
        }
        return SEGMENT_PREFIX + index + suffixName;
    }


    public String getInitSegmentName() {
        String suffixName = "";
        if (!TextUtils.isEmpty(initSegmentUri)) {
            Uri uri = Uri.parse(initSegmentUri);
            String fileName = uri.getLastPathSegment();
            if (!TextUtils.isEmpty(fileName)) {
                fileName = fileName.toLowerCase();
                suffixName = getSuffixName(fileName);
            }
        }
        return INIT_SEGMENT_PREFIX + index + suffixName;
    }

    public String toString() {
        return "duration=" + duration + ", index=" + index + ", name=" + name;
    }

    public static String getSuffixName(String name) {
        if (TextUtils.isEmpty(name)) {
            return "";
        }
        int dotIndex = name.lastIndexOf('.');
        return dotIndex >= 0 ? name.substring(dotIndex) : "";
    }
    @Override
    public int compareTo(M3U8Seg object) {
        return name.compareTo(object.name);
    }



}

